<template>
<div style="padding: 10px">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm"  style="width: 100%">
  <el-form-item label="用户学号/工号" prop="identity_id">
    <el-input v-model="ruleForm.identity_id"></el-input>
  </el-form-item>
   <el-form-item label="用户密码" prop="password">
    <el-input v-model="ruleForm.password"></el-input>
  </el-form-item>
  <el-form-item label="用户权限" prop="authority">
    <el-input v-model="ruleForm.authority"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
</div>
</template>
<script>
  export default {
    data() {
      return {
          
        ruleForm: {
          identity_id: '',
          password: '',
          authority:''
        },
        rules: {
          identity_id: [
            { required: true, message: '请输入用户学号/工号', trigger: 'blur' },
            { min: 3, max: 20, message: '长度在 3 到 10 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入用户密码', trigger: 'blur' },
            { min: 3, max: 20, message: '长度在 3 到 10 个字符', trigger: 'blur' }
          ],
          authority: [
            { required: true, message: '请输入用户权限', trigger: 'blur' },
            { min: 1, max: 10, message: '长度为1-10个字符', trigger: 'blur' }
          ],
          
        }
      };
    },
    methods: {
    
      submitForm(formName) {
          const _this=this
          var qs = require('qs');
        this.$refs[formName].validate((valid) => {
          if (valid) {
              axios({
            method:"post",
            url:"http://39.99.160.25:8088/users/web/register",
           data:qs.stringify(this.ruleForm),
            headers:
            {
              "Authorization":sessionStorage.getItem('token'),
            }
            
        }).then(function (resp){
             if(resp.data.data.msg=='注册成功'){
                  _this.$alert('《'+_this.ruleForm.identity_id+'》添加成功！', '消息', {
          confirmButtonText: '确定',
          callback: action => {
                _this.$router.push('/users')
              // window.location.reload();
          }  
        });
          
             
          }
          else{
               _this.$alert('《'+_this.ruleForm.identity_id+'》添加失败！', '消息', {
          confirmButtonText: '确定',
        });
          }
        });
          }
        })
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
    
    
  }
</script>